/**
 * 两种状态（点击，长按）按钮的基类
 * ibio-develop
 * 2008-11-24 15:14
 */
package com.ibio8.components.buttons {
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.events.Event;
	import flash.utils.Timer;
	/**
	 * 两种状态（点击，长按）按钮的基类。
	 * 
	 * <p>
	 * 提供 4 帧包括点击/长按的基本按钮。通常可以和以下类配合使用：
	 * </p>
	 * <ul>
	 * <li>com.ibio8.components.helpers.BaseScrollBar</li>
	 * </ul>
	 * 
	 * @see com.ibio8.components.helpers.BaseScrollBar	BaseScrollBar
	 */
	public class BaseStateButton extends MovieClip {
		/** 每次 call 的间隔和首次延迟间隔，因为此类不太可能直接 new，所以这里没有加 static **/
		public var DELTA_TIME:Number = 0.05;
		/** 每次 call 需要达到的的时间间隔 **/
		public var DELAY_TIME:Number = 0.3;
		/** @private **/
		protected var m_btn:MovieClip;
		/** @private **/
		//是否禁用
		protected var m_enabled:Boolean = true;
		/** @private **/
		protected var m_clickDoLater:Function;
		/** @private **/
		protected var m_clickDoLaterParams:Array;
		/** @private **/
		protected var m_currTime:Number;
		/** @private **/
		protected var m_timer:Timer;
		
		/**
		 * 构造函数
		 * @param	button	库里当前的按钮 MC 或者是舞台上还未添加到显示列表的 MC
		 */
		public function BaseStateButton(button:MovieClip) {
			m_btn = button;
			m_btn.stop();
			m_btn.buttonMode = true;
			m_btn.mouseChildren = false;
			m_btn.addEventListener(MouseEvent.ROLL_OVER, onRollOverHandler);
			m_btn.addEventListener(MouseEvent.ROLL_OUT, onRollOutHandler);
			m_btn.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
			m_btn.addEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
			m_timer = new Timer(DELTA_TIME * 1000);
			m_timer.addEventListener(TimerEvent.TIMER, onTimerHandler);
			m_btn.addEventListener(Event.REMOVED_FROM_STAGE, onBtnRemovedHandler);
		}
		
		/**
		 * 如果是直接 new BaseStateButton，则可通过 getter 返回当前的 target MC
		 */
		public function get self():MovieClip {
			return m_btn;
		}
		
		/**
		 * 点击后的回调函数
		 * @param	doLater	函数名称
		 * @param	...rest	该函数的参数
		 */
		public function onClick(doLater:Function, ...rest):void {
			m_clickDoLater = doLater;
			m_clickDoLaterParams = rest;
		}
		
		/**
		 * 是否禁用此按钮
		 */
		public override function set enabled(value:Boolean):void {
			if (m_enabled == value) {
				return;
			}
			m_enabled = value;
			//激活
			onBtnRollOut();
		}
		
		public override function get enabled():Boolean{
			return m_enabled;
		}
		
		/** @private **/
		protected function onRollOverHandler(e:MouseEvent):void {
			m_btn.gotoAndStop(2);
		}
		
		/** @private **/
		protected function onMouseDownHandler(e:MouseEvent):void {
			m_btn.gotoAndStop(3);
			isLiveClick = true;
			onMouseClick();
		}
		
		/** @private **/
		protected function onMouseUpHandler(e:MouseEvent):void {
			isLiveClick = false;
			onBtnRollOut();
			
		}
		
		/** @private **/
		protected function onRollOutHandler(e:MouseEvent):void {
			isLiveClick = false;
			onBtnRollOut();
		}
		
		/** @private **/
		protected function onBtnRollOut():void {
			if (m_enabled) {
				m_btn.gotoAndStop(1);
				m_btn.mouseEnabled = true;
			}else {
				m_btn.gotoAndStop(4);
				m_btn.mouseEnabled = false;
			}
		}
		
		/** @private **/
		//是否一直按着
		protected function set isLiveClick(value:Boolean):void{
			if(value){
				m_currTime = 0;
				m_timer.start();
			}else{
				m_timer.stop();
			}
		}
		
		/** @private **/
		protected function onMouseClick():void {
			if (m_clickDoLater != null) {
				m_clickDoLater.apply(null, m_clickDoLaterParams);
			}
		}
		
		/** @private **/
		protected function onTimerHandler(e:TimerEvent):void {
			if(m_currTime >= DELAY_TIME){
				onMouseClick();
			}else {
				m_currTime += DELTA_TIME;
			}
		}
		
		/** @private **/
		protected function onBtnRemovedHandler(e:Event):void {
			m_btn.removeEventListener(MouseEvent.ROLL_OVER, onRollOverHandler);
			m_btn.removeEventListener(MouseEvent.ROLL_OUT, onRollOutHandler);
			m_btn.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
			m_btn.removeEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
			m_timer.removeEventListener(TimerEvent.TIMER, onTimerHandler);
			m_btn.removeEventListener(Event.REMOVED_FROM_STAGE, onBtnRemovedHandler);
		}
	}
}